package org.ccheng.xztx.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.cron.pattern.CronPattern;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.ccheng.xztx.domain.UserInfo;
import org.ccheng.xztx.service.SportService;
import org.ccheng.xztx.service.UserService;
import org.ccheng.xztx.util.Api;
import org.ccheng.xztx.util.ApiConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SportServiceImpl implements SportService {

    private static final String I = "[仙池]：";

    @Autowired
    private UserService userService;

    public JSONObject getSportUserList() {
        String response = Api.get(ApiConstants.API_SPORT_INDEX);

        String num = ReUtil.get("(?<=挑战次数:)\\d{1,3}", response, 0);

        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOpt("num", num);

        for (int i = 1; i < 4; i++) {
            String userId = ReUtil.get(String.format("(?<=>%s\\..{0,250}userId=)\\d+(?='>)", i), response, 0);
            jsonObject.append("user", userService.findById(userId));
        }

        return jsonObject;
    }

    public void PK(Long myAtk) {

        JSONObject jsonObject = getSportUserList();

        UserInfo lastEnemy = null;

        // 获取所有对手
        JSONArray enemyList = jsonObject.getJSONArray("user");
        for (JSONObject o : enemyList.toList(JSONObject.class)) {
            UserInfo enemy = JSONUtil.toBean(o, UserInfo.class);

            Long enemyAtk = Long.parseLong(enemy.getAttack());

            if (myAtk > enemyAtk) {
                lastEnemy = enemy;
                break;
            }
        }

        if (lastEnemy == null) {
            Api.out(I, "当前没有能够战胜的目标，等待5分钟后重试");
            return;
        }

        Api.out(I, "尝试进攻目标[", lastEnemy.getUsername(), "]   ID:", lastEnemy.getId());

        Map<String, Object> params = MapUtil.newHashMap();
        params.put("otherId", lastEnemy.getId());
        String response = Api.get(ApiConstants.API_SPORT_PK, params);

        String result = ReUtil.get("(?<=<br/>)\\S+(?=<a href='\\S+/pk/processInfo.asp)|(?<=<div class=\"gaps normal\">).{0,20}(?=<br/><)", response, 0);
        Api.out(I, "挑战结果：" + result);
    }

    public void auto() {
        Api.out(I, "开始自动挑战");

        CronPattern pattern = new CronPattern("* 0-59 7-23 * * ? *");
        if (!pattern.match(System.currentTimeMillis(), true)) {
            Api.out(I, "当前时间不在活动时间内,请 07:00 - 23:59 尝试");
            return;
        }

        JSONObject jsonObject = getSportUserList();

        // 可挑战次数
        int num = jsonObject.getInt("num");

        UserInfo userInfo = userService.findById(null);
        Long myAtk = Long.parseLong(userInfo.getAttack());

        for (int i = 0; i < num; i++) {
            PK(myAtk);
        }

        Api.out(I, "挑战结束");
        Api.out(I, "共执行：" + num + "次");
    }

}
